import store from '@/store'
import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: 'home'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView
  },
  {
    path: '/goods',
    name: 'Goods',
    component: () => import('../views/Goods.vue')
  },
  {
    path: '/user',
    name: 'User',
    redirect:'/user/cart',
    component: () => import('../views/User.vue'),
    children:[
      {
        path:'/user/cart',
        name:'cart',
        component:()=>import('../components/user/Cart.vue')
      },
      {
        path:'/user/jifenDetail',
        name:'jifenDetail',
        component:()=>import('../components/user/jifenDetail.vue')
      }
    ],
    // 独享守卫
    beforeEnter:(from,to,next)=>{
      let token =  localStorage.getItem("x-auth-token")
      if (token) {
        next();
      }else{
        store.dispatch("showToast/asyncIsShowToast",{
          msg:'请先登录！！',
          type:'danger'
        })
      }
    }
  },
  {
    path: '/order',
    name: 'Order',
    component: () => import('../views/Order.vue')
  },
  {
    path: '/free',
    name: 'Free',
    component: () => import('../views/Free.vue')
  },
  {
    path: '/details',
    name: 'Details',
    component: () => import(/* webpackChunkName: "details" */ '../views/Details.vue')
  },
  {
    path:'*',
    component:()=>import('../components/Error.vue')
  }
]



const router = new VueRouter({
  mode: 'history',
  routes
})

// 全局守卫
// router.beforeEach((to, from, next) => {
//   if (to.path == "/user") {
//     let token = localStorage.getItem("x-auth-token")
//     if (token) {
//       next()
//     } else {
//       store.dispatch("showToast/asyncIsShowToast",{
//         msg:'请先登录！！',
//         type:'danger'
//       })
//     }
//   } else {
//     next()
//   }
// })

export default router
